<?php
/*
 * @Description    : 日志发送
 * @Author         : QianLong
 * @Date           : 2023-01-11 12:13:55
 * @LastEditors    : QianLong
 * @Site           : http://21ds.cn
 * @LastEditTime   : 2024-01-16 16:58:16
 */

declare(strict_types=1);

namespace App\Controller;

use App\Amqp\Producer\LogProducer;
use App\Common\RedisCache;
use App\Constants\Status;
use App\Exception\ApiException;
use App\Job\SignFailAddJob;
use Qianlong\Validate\Validate;

class LogController extends AbstractController
{
  public function push()
  {
    $valitor = new Validate();
    $valitor->addColumn('lsi_id')->required();
    if ($valitor && !$this->validate($valitor)) {
      throw new ApiException(Status::CODE_BAD_REQUEST, $valitor->getError()->__toString());
    }
    unset($valitor);
    $param = $this->request->all();
    $redisCache = new RedisCache;
    $serverInfo = $redisCache->getLogSite($param['lsi_id']);
    $commonService = new \App\Service\Common;
    $newSign = $commonService->devSignGeneral($param, $serverInfo['server_secret']);
    $newSign2 = $commonService->generateSignature($param, $serverInfo['server_secret']);
    if ($param['sign'] != $newSign && $param['sign'] != $newSign2['sign']) {
      $logData['url'] = $this->request->fullUrl();
      $logData['remoteIp'] = $this->dataResource->getClientIp($this->request);
      $logData['user_sign'] = $param['sign'];
      $logData['uid'] = $serverInfo['uid'];
      $logData['server_sign'] = $newSign2['sign'];
      $logData['server_query'] = $newSign2['query'];
      queue_push(new SignFailAddJob($logData));
      return $this->response->json(Status::CODE_FAIL, 'sign签名校验失败');
    }
    unset($newSign, $newSign2);
    $userInfo = $redisCache->getUserInfoById($serverInfo['uid']);
    if (empty($userInfo) || $userInfo['status'] != 1) {
      throw new ApiException(Status::CODE_FAIL, '用户状态异常或被禁，服务不可用，如有疑问请联系柠聚开发平台客服咨询');
    }
    if ($serverInfo['status'] != 1) {
      throw new ApiException(Status::CODE_FAIL, '当前配置状态不可用，如需使用，请登录柠聚开发平台启用后重试');
    }
    $logData = [];
    $atMobiles = '';
    if (isset($param['atMobiles']) && !empty($param['atMobiles'])) {
      $atMobiles = $param['atMobiles'];
    }
    unset($param['atMobiles']);
    foreach ($param as $key => $vo) {
      if (in_array($key, $serverInfo['fields'])) {
        // // if ($commonService->is_base64_encoded($vo)) {
        //   $vo = base64_decode($vo)? base64_decode($vo):$vo;
        // // }
        $logData[$key] = is_array($vo) ? json_encode($vo, JSON_UNESCAPED_UNICODE) : $vo;
      }
    }
    unset($commonService);
    if (!empty($logData)) {
      $remoteIp = $this->dataResource->getClientIp($this->request);
      $param['remote_ip'] = (isset($param['remote_ip']) && !empty($param['remote_ip'])) ? $param['remote_ip'] : $remoteIp;
      $jobData['atMobiles'] = $atMobiles;
      $jobData['logData'] = $logData;
      $jobData['param'] = $param;
      $jobData['tag_type'] = '0';
      $jobData['lsi_id'] = $param['lsi_id'];
      $jobData['request_id'] = isset($param['request_id']) ? $param['request_id'] : '';
      $jobData['serverInfo'] = $serverInfo;
      // $message = new LogProducer($jobData);
      // rabbit_push($message);
      (new \App\Job\PushLogJob($jobData))->handle();
      return $this->response->json(Status::CODE_OK, 'success', []);
    }
    return $this->response->json(Status::CODE_FAIL, '无有效数据', []);
  }
  public function push_v2()
  {
    $valitor = new Validate();
    $valitor->addColumn('lsi_id')->required();
    if ($valitor && !$this->validate($valitor)) {
      throw new ApiException(Status::CODE_BAD_REQUEST, $valitor->getError()->__toString());
    }
    unset($valitor);
    $param = $this->request->all();
    unset($param['ignore_rand_str']);
    $redisCache = new RedisCache;
    $serverInfo = $redisCache->getLogSite(base64_decode($param['lsi_id']));
    $commonService = new \App\Service\Common;
    $newSign = $commonService->devSignGeneral($param, $serverInfo['server_secret']);
    $newSign2 = $commonService->generateSignature($param, $serverInfo['server_secret']);
    if ($param['sign'] != $newSign && $param['sign'] != $newSign2['sign']) {
      $logData['url'] = $this->request->fullUrl();
      $logData['remoteIp'] = $this->dataResource->getClientIp($this->request);
      $logData['user_sign'] = $param['sign'];
      $logData['uid'] = $serverInfo['uid'];
      $logData['server_sign'] = $newSign2['sign'];
      $logData['server_query'] = $newSign2['query'];
      queue_push(new SignFailAddJob($logData));
      return $this->response->json(Status::CODE_FAIL, 'sign签名校验失败');
    }
    unset($newSign, $newSign2);
    $userInfo = $redisCache->getUserInfoById($serverInfo['uid']);
    if (empty($userInfo) || $userInfo['status'] != 1) {
      throw new ApiException(Status::CODE_FAIL, '用户状态异常或被禁，服务不可用，如有疑问请联系柠聚开发平台客服咨询');
    }
    if ($serverInfo['status'] != 1) {
      throw new ApiException(Status::CODE_FAIL, '当前配置状态不可用，如需使用，请登录柠聚开发平台启用后重试');
    }
    $logData = [];
    $atMobiles = '';
    if (isset($param['atMobiles']) && !empty($param['atMobiles'])) {
      $atMobiles = $param['atMobiles'];
    }
    unset($param['atMobiles']);
    foreach ($param as $key => $vo) {
      $param[$key] = base64_decode($vo);
    }
    foreach ($param as $key => $vo) {
      if (in_array($key, $serverInfo['fields'])) {
        $logData[$key] = is_array($vo) ? json_encode($vo, JSON_UNESCAPED_UNICODE) : $vo;
      }
    }
    unset($commonService);
    if (!empty($logData)) {
      $remoteIp = $this->dataResource->getClientIp($this->request);
      $param['remote_ip'] = (isset($param['remote_ip']) && !empty($param['remote_ip'])) ? $param['remote_ip'] : $remoteIp;
      $jobData['atMobiles'] = $atMobiles;
      $jobData['logData'] = $logData;
      $jobData['param'] = $param;
      $jobData['tag_type'] = '0';
      $jobData['lsi_id'] = $param['lsi_id'];
      $jobData['request_id'] = isset($param['request_id']) ? $param['request_id'] : '';
      $jobData['serverInfo'] = $serverInfo;
      // $message = new LogProducer($jobData);
      // rabbit_push($message);
      (new \App\Job\PushLogJob($jobData))->handle();
      return $this->response->json(Status::CODE_OK, 'success', []);
    }
    return $this->response->json(Status::CODE_FAIL, '无有效数据', []);
  }
}
